﻿// MedicalRulesEngine.Domain/Entities/MetadataRule.cs
using System.ComponentModel.DataAnnotations;

namespace MedicalRulesEngine.Domain.Entities;

/// <summary>
/// 元数据驱动的规则实体
/// 通过元数据JSON存储灵活的规则配置，支持动态扩展
/// </summary>
public class MetadataRule
{
    /// <summary>
    /// 规则唯一标识符
    /// </summary>
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 规则名称
    /// </summary>
    [Required, MaxLength(200)]
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 规则编码，用于业务标识
    /// </summary>
    [Required, MaxLength(50)]
    public string Code { get; set; } = string.Empty;

    /// <summary>
    /// 规则描述
    /// </summary>
    [MaxLength(500)]
    public string? Description { get; set; }

    /// <summary>
    /// 所属工作流名称
    /// </summary>
    [Required, MaxLength(100)]
    public string WorkflowName { get; set; } = string.Empty;

    /// <summary>
    /// 规则分类
    /// </summary>
    [Required, MaxLength(50)]
    public string Category { get; set; } = string.Empty;

    /// <summary>
    /// 规则子分类
    /// </summary>
    [MaxLength(50)]
    public string? SubCategory { get; set; }

    /// <summary>
    /// 规则优先级
    /// </summary>
    public RulePriority Priority { get; set; } = RulePriority.Medium;

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool IsEnabled { get; set; } = true;

    /// <summary>
    /// 规则表达式
    /// </summary>
    public string? Expression { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime? UpdatedAt { get; set; }

    /// <summary>
    /// 创建人
    /// </summary>
    [MaxLength(100)]
    public string? CreatedBy { get; set; }

    /// <summary>
    /// 更新人
    /// </summary>
    [MaxLength(100)]
    public string? UpdatedBy { get; set; }

    /// <summary>
    /// 元数据JSON存储，支持动态配置
    /// 包含UI配置、验证规则、业务属性等
    /// </summary>
    public string? MetadataJson { get; set; }

    /// <summary>
    /// 规则版本号
    /// </summary>
    public int Version { get; set; } = 1;

    /// <summary>
    /// 规则状态
    /// </summary>
    public RuleStatus Status { get; set; } = RuleStatus.Draft;

    // 导航属性
    public virtual ICollection<RuleCondition> Conditions { get; set; } = new List<RuleCondition>();
    public virtual ICollection<RuleAction> Actions { get; set; } = new List<RuleAction>();
    public virtual ICollection<RuleTag> Tags { get; set; } = new List<RuleTag>();
    public virtual ICollection<RuleExecutionLog> ExecutionLogs { get; set; } = new List<RuleExecutionLog>();
}

/// <summary>
/// 规则条件实体 - 支持嵌套和复杂逻辑
/// </summary>
public class RuleCondition
{
    /// <summary>
    /// 条件唯一标识符
    /// </summary>
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 所属规则ID
    /// </summary>
    [Required]
    public string RuleId { get; set; } = string.Empty;

    /// <summary>
    /// 字段路径，支持嵌套对象访问，如：patient.age
    /// </summary>
    [Required, MaxLength(200)]
    public string FieldPath { get; set; } = string.Empty;

    /// <summary>
    /// 操作符：==, !=, >, <, >=, <=, contains, in, between等
    /// </summary>
    [Required, MaxLength(50)]
    public string Operator { get; set; } = string.Empty;

    /// <summary>
    /// 比较值，支持JSON格式存储复杂数据
    /// </summary>
    public string? Value { get; set; }

    /// <summary>
    /// 逻辑操作符：AND, OR
    /// </summary>
    [MaxLength(10)]
    public string LogicalOperator { get; set; } = "AND";

    /// <summary>
    /// 条件排序
    /// </summary>
    public int Order { get; set; }

    /// <summary>
    /// 父条件ID，用于构建条件树
    /// </summary>
    public string? ParentConditionId { get; set; }

    /// <summary>
    /// 条件元数据，存储UI配置等信息
    /// </summary>
    public string? MetadataJson { get; set; }

    /// <summary>
    /// 条件分组，用于复杂逻辑分组
    /// </summary>
    [MaxLength(50)]
    public string? GroupName { get; set; }

    // 导航属性
    public virtual MetadataRule Rule { get; set; } = null!;
    public virtual RuleCondition? ParentCondition { get; set; }
    public virtual ICollection<RuleCondition> ChildConditions { get; set; } = new List<RuleCondition>();
}

/// <summary>
/// 规则动作实体 - 定义规则执行后的动作
/// </summary>
public class RuleAction
{
    /// <summary>
    /// 动作唯一标识符
    /// </summary>
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 所属规则ID
    /// </summary>
    [Required]
    public string RuleId { get; set; } = string.Empty;

    /// <summary>
    /// 动作名称
    /// </summary>
    [Required, MaxLength(100)]
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 动作类型：Success, Failure, Always
    /// </summary>
    [Required, MaxLength(50)]
    public string ActionType { get; set; } = string.Empty;

    /// <summary>
    /// 动作配置JSON，存储动作的具体参数
    /// </summary>
    public string? ConfigJson { get; set; }

    /// <summary>
    /// 动作排序
    /// </summary>
    public int Order { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool IsEnabled { get; set; } = true;

    // 导航属性
    public virtual MetadataRule Rule { get; set; } = null!;
}

/// <summary>
/// 规则标签实体 - 用于规则分类和检索
/// </summary>
public class RuleTag
{
    /// <summary>
    /// 标签唯一标识符
    /// </summary>
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 所属规则ID
    /// </summary>
    [Required]
    public string RuleId { get; set; } = string.Empty;

    /// <summary>
    /// 标签名称
    /// </summary>
    [Required, MaxLength(50)]
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 标签值
    /// </summary>
    [MaxLength(100)]
    public string? Value { get; set; }

    /// <summary>
    /// 标签颜色，用于UI显示
    /// </summary>
    [MaxLength(20)]
    public string? Color { get; set; }

    // 导航属性
    public virtual MetadataRule Rule { get; set; } = null!;
}

/// <summary>
/// 规则执行日志实体
/// </summary>
public class RuleExecutionLog
{
    /// <summary>
    /// 日志唯一标识符
    /// </summary>
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 所属规则ID
    /// </summary>
    [Required]
    public string RuleId { get; set; } = string.Empty;

    /// <summary>
    /// 执行时间
    /// </summary>
    public DateTime ExecutedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 执行耗时（毫秒）
    /// </summary>
    public long ExecutionTimeMs { get; set; }

    /// <summary>
    /// 是否执行成功
    /// </summary>
    public bool IsSuccess { get; set; }

    /// <summary>
    /// 执行结果
    /// </summary>
    public string? Result { get; set; }

    /// <summary>
    /// 错误信息
    /// </summary>
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 输入数据JSON
    /// </summary>
    public string? InputDataJson { get; set; }

    /// <summary>
    /// 执行上下文JSON
    /// </summary>
    public string? ContextJson { get; set; }

    // 导航属性
    public virtual MetadataRule Rule { get; set; } = null!;
}

/// <summary>
/// 字段定义实体 - 元数据驱动的字段配置
/// </summary>
public class FieldDefinition
{
    /// <summary>
    /// 字段唯一标识符
    /// </summary>
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 字段名称
    /// </summary>
    [Required, MaxLength(100)]
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 字段路径，支持嵌套访问
    /// </summary>
    [Required, MaxLength(200)]
    public string Path { get; set; } = string.Empty;

    /// <summary>
    /// 字段显示名称
    /// </summary>
    [Required, MaxLength(100)]
    public string DisplayName { get; set; } = string.Empty;

    /// <summary>
    /// 字段描述
    /// </summary>
    [MaxLength(500)]
    public string? Description { get; set; }

    /// <summary>
    /// 字段数据类型
    /// </summary>
    [Required, MaxLength(50)]
    public string DataType { get; set; } = string.Empty;

    /// <summary>
    /// 字段分类
    /// </summary>
    [Required, MaxLength(50)]
    public string Category { get; set; } = string.Empty;

    /// <summary>
    /// 是否必填
    /// </summary>
    public bool IsRequired { get; set; }

    /// <summary>
    /// 是否可用于条件
    /// </summary>
    public bool IsConditionable { get; set; } = true;

    /// <summary>
    /// 支持的操作符JSON数组
    /// </summary>
    public string? SupportedOperatorsJson { get; set; }

    /// <summary>
    /// 字段配置JSON，包含验证规则、UI配置等
    /// </summary>
    public string? ConfigJson { get; set; }

    /// <summary>
    /// 排序
    /// </summary>
    public int Order { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}

/// <summary>
/// 操作符定义实体 - 元数据驱动的操作符配置
/// </summary>
public class OperatorDefinition
{
    /// <summary>
    /// 操作符唯一标识符
    /// </summary>
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 操作符代码
    /// </summary>
    [Required, MaxLength(50)]
    public string Code { get; set; } = string.Empty;

    /// <summary>
    /// 操作符显示名称
    /// </summary>
    [Required, MaxLength(100)]
    public string DisplayName { get; set; } = string.Empty;

    /// <summary>
    /// 操作符描述
    /// </summary>
    [MaxLength(500)]
    public string? Description { get; set; }

    /// <summary>
    /// 支持的数据类型JSON数组
    /// </summary>
    public string? SupportedDataTypesJson { get; set; }

    /// <summary>
    /// 操作符分类
    /// </summary>
    [MaxLength(50)]
    public string? Category { get; set; }

    /// <summary>
    /// 是否需要值输入
    /// </summary>
    public bool RequiresValue { get; set; } = true;

    /// <summary>
    /// 是否支持多值
    /// </summary>
    public bool SupportsMultipleValues { get; set; }

    /// <summary>
    /// 表达式模板，用于生成规则表达式
    /// </summary>
    public string? ExpressionTemplate { get; set; }

    /// <summary>
    /// 排序
    /// </summary>
    public int Order { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}

/// <summary>
/// 规则模板实体 - 预定义的规则模板
/// </summary>
public class RuleTemplate
{
    /// <summary>
    /// 模板唯一标识符
    /// </summary>
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 模板名称
    /// </summary>
    [Required, MaxLength(200)]
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 模板编码
    /// </summary>
    [Required, MaxLength(50)]
    public string Code { get; set; } = string.Empty;

    /// <summary>
    /// 模板描述
    /// </summary>
    [MaxLength(500)]
    public string? Description { get; set; }

    /// <summary>
    /// 模板分类
    /// </summary>
    [Required, MaxLength(50)]
    public string Category { get; set; } = string.Empty;

    /// <summary>
    /// 模板配置JSON，包含默认条件、动作等
    /// </summary>
    public string? ConfigJson { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool IsEnabled { get; set; } = true;

    /// <summary>
    /// 排序
    /// </summary>
    public int Order { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}

/// <summary>
/// 规则优先级枚举
/// </summary>
public enum RulePriority
{
    /// <summary>
    /// 低优先级
    /// </summary>
    Low = 1,

    /// <summary>
    /// 中等优先级
    /// </summary>
    Medium = 2,

    /// <summary>
    /// 高优先级
    /// </summary>
    High = 3,

    /// <summary>
    /// 关键优先级
    /// </summary>
    Critical = 4
}

/// <summary>
/// 规则状态枚举
/// </summary>
public enum RuleStatus
{
    /// <summary>
    /// 草稿状态
    /// </summary>
    Draft = 0,

    /// <summary>
    /// 待审核状态
    /// </summary>
    PendingReview = 1,

    /// <summary>
    /// 已发布状态
    /// </summary>
    Published = 2,

    /// <summary>
    /// 已停用状态
    /// </summary>
    Disabled = 3,

    /// <summary>
    /// 已归档状态
    /// </summary>
    Archived = 4
}
